﻿myApp.controller("RSSCtrl", function ($scope, $post, $http, $resource, $routeParams, $restUrls, $rootScope, $location) {
    
   

    $scope.page = 0;


    $scope.list = [];
    $scope.distance = 0;
    $scope.paginating = false;
    $scope.enabled = true;

    $scope.loadList = function () {
        if (!$scope.enabled || true === $scope.paginating)
            return;

        var parameters = {
            pageNum: $scope.page++,
            pageSize: 20
        };

        $scope.paginating = true;

        $resource($restUrls["mySubscriptions"] + '&pageNum=:pageNum&pageSize=:pageSize').get(parameters).$promise.then(function (data) {


                        for (var i = 0; i < data.data.length; i++) {
                            $scope.list.push(data.data[i]);
                        }
                        $scope.enabled = data.data.length > 0 && data.data.length === parameters.pageSize;
                        $scope.paginating = false;
                       
        });

    };

   

    var getMyCategoryList = function () {
        $resource($restUrls["getMyCategoryList"]).get().$promise.then(function (data) {


            $scope.categories = data.data;

            if (!$scope.categories || $scope.categories.length == 0) {
                $resource($restUrls["getCategoryList"]).get().$promise.then(function (data) {
                    $scope.allcategories = data.data;
                    for (var i = 0; i < $scope.allcategories.length; i++) {
                        $scope.allcategories[i].chosen = $scope.allcategories[i].chosen == '0' ? false : true;
                    }
                });
            }
            else {
                $scope.page = 0;
                $scope.list = [];
				$scope.enabled = true;
                $scope.loadList();
            }



        });
    }
    getMyCategoryList();




    $scope.skip_category = false;

    $scope.set_skip_category = function (flag) {
        $scope.skip_category = flag;
    }

    $scope.category_select = function (category) {
        category.chosen = !category.chosen;
    }
    $scope.set_bind_category = function () {
        var selected = new Array();
        for (var i = 0; i < $scope.allcategories.length; i++) {

            if ($scope.allcategories[i].chosen) {
                selected.push($scope.allcategories[i].term_id);
            }
        }
        if (selected.length == 0)
            return;

        $post($restUrls["bindCategoryList"],
            {
                categories: selected.join(",")
            })
            .success(function (result) {
                getMyCategoryList();
            }).error(function (data, status, headers, config) {
            });



    }

    $scope.show_category_boxes = function(){
        $scope.skip_category = false;
        $scope.categories = null;
         $resource($restUrls["getCategoryList"]).get().$promise.then(function (data) {
                    $scope.allcategories = data.data;
                    for (var i = 0; i < $scope.allcategories.length; i++) {
                        $scope.allcategories[i].chosen = $scope.allcategories[i].chosen == '0' ? false : true;
                    }
                });
    }
    

});
